/**
 * Main.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.activity.Main
 * xiaocq Create at 2012-7-11 下午2:18:26
 */
package com.xiaocq.wordsgod;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.xiaocq.wordsgod.db.WordSetUtil;

/**
 * com.xiaocq.wordsgod.activity.Main
 * @author Mystory
 * Create at 2012-7-11 下午2:18:26
 */
public class Main extends Activity{
	private static final String TAG = "Main";
	
	private static final String IS_FIRST_LAUNCH = "isFirstLaunch";
	private static final String PREFERENCE = "preferenceFile";
	
	private static final int IMPORT_DIALOG = 1;
	
	private boolean isFirstLaunch = true;
	
	private long exitTime;
	
	private Button mTest, mWrongSet, mStatistic, mSetting;
	private SharedPreferences pref;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		
		initComponents();
		
		pref = getSharedPreferences(PREFERENCE, MODE_PRIVATE);
		
		isFirstLaunch = pref.getBoolean(IS_FIRST_LAUNCH, true);
		
		if(isFirstLaunch){
			showDialog(IMPORT_DIALOG);
		}
		
	}
	
	private void initComponents(){
		mTest = (Button) findViewById(R.id.main_gotoTest);
		mWrongSet = (Button) findViewById(R.id.main_gotoWrongSet);
		mStatistic = (Button) findViewById(R.id.main_gotoStatistic);
		mSetting = (Button) findViewById(R.id.main_gotoSetting);
		
		mTest.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(isFirstLaunch){
					showDialog(IMPORT_DIALOG);
					return;
				}
				Intent intent = new Intent();
				intent.setClass(Main.this, GenerateControl.class);
				startActivity(intent);
			}
		});
		
		mWrongSet.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Main.this, WrongSet.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
			if((System.currentTimeMillis() - exitTime) > 1500){
				Toast.makeText(getApplicationContext(), R.string.click_again_to_exit, 1000).show();
				exitTime = System.currentTimeMillis();
			}else{
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(this);
		
		switch (id) {
		case IMPORT_DIALOG:
			builder.setMessage(R.string.importMsg);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					ImportTask task = new ImportTask();
					task.execute();
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			return builder.create();
		default:
			break;
		}
		return super.onCreateDialog(id);
	}
	
	private class ImportTask extends AsyncTask<Void, Void, Void>{
		private WordSetUtil wordSetUtil;
		private ProgressDialog progressDialog;
		
		private long start,finish;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog = new ProgressDialog(Main.this);
			progressDialog.setMessage(getText(R.string.i_am_loading));
			progressDialog.setCancelable(false);
			progressDialog.show();
			
			wordSetUtil = new WordSetUtil(Main.this);
			wordSetUtil.open();
			
			start = System.currentTimeMillis();
		}

		@Override
		protected Void doInBackground(Void... params) {
			wordSetUtil.importDatabase();
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			finish = System.currentTimeMillis();
			
			wordSetUtil.close();
			
			isFirstLaunch = false;
			Editor editor = pref.edit();
			editor.putBoolean(IS_FIRST_LAUNCH, isFirstLaunch);
			editor.commit();
			
			progressDialog.dismiss();
			
			Toast.makeText(Main.this, R.string.loading_completed, 1000).show();
			
			Log.d(TAG, "加载数据库共耗时 " + (finish - start) + "ms");
		}
		
	}
}
